﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TVKZUAB
    {

        public TVKZUAB()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKZUAB();
            MESSAGES += "ZBDEL;";
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            try
            {
                TBMain.Clear();
                if (ORDERNUM.Length < 11)
                    return;
                string order = ORDERNUM;
                KRS = order.Substring(1, 2);
                LFD = order.Substring(3, 6);
                POS = order.Substring(9, 3);
                dwData = TBMain.NewRow();
                dwData["VKZMANDANT"] = app.Mandant;
                dwData["VKZSA"] = VKZSA;
                dwData["VKZAUFKRS"] = KRS;
                dwData["VKZAUFLFD"] = LFD;
                dwData["VKZAUFPOS"] = POS;
                SResult = TMV.QueryVKZUAB(dwData, 2);
                MergeData(SResult, true); 
                TMV.GetInfo();
                INFO = MV.INFO;
                if (!IsAuto)
                    return;
                if (TBMain.Rows.Count > 0)
                    return;

                PM["ORDER"] = ORDERNUM;
                PM["VKZSA"] = VKZSA;
                PM["KRS"] = KRS;
                PM["LFD"] = LFD;
                PM["POS"] = POS;
                CREATE(PM); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public override void OnEXCOMChg(string com)
        {
            if (dwMaster == null)
                return;
            if (com == "")
                return;
            if (com == "0")
                return;
            if (com == "00000000")
                return;

            com = com.PadRight(12);
            if (TMV.VKZPROV1 > 0)
                return;
            TMV.VKZVERTNR1 = Helpers.Helper.Right("00000000" + com.Substring(0, 8), 8);
            TMV.VKZPROV1 = Convert.ToDouble(Helpers.Helper.Right("0000" + com.Substring(8, 4), 4));
            TMV.UVKZUAB201(dwMaster);
            base.OnEXCOMChg(com);
        }
        public override void OnEXBONChg(string com)
        {
            if (dwMaster == null)
                return;
            if (com == "")
                return;
            if (com == "0")
                return;
            if (com == "00000000")
                return;

            com = com.PadRight(12);
            if (TMV.VKZPROV2 > 0)
                return;
            TMV.VKZVERTNR2 = Helpers.Helper.Right("00000000" + com.Substring(0, 8), 8);
            TMV.VKZPROV2 = Convert.ToDouble(Helpers.Helper.Right("0000" + com.Substring(8, 4), 4));
            TMV.UVKZUAB201(dwMaster);
            base.OnEXBONChg(com);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                if (!TMV.GetInfo())
                    return;
                INFO = MV.INFO;
                TMV.UpdateVKZUAB(dwMaster, app.company, 0);
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(app.User + "你", ex.Message);
            }
        }

        public override void OnSave()
        {
            try
            {
                if (!IsSaved)
                    return;
                if (dwMaster == null)
                    return;
                if (!TMV.GetInfo())
                    return;
                INFO = MV.INFO;
                TMV.UpdateVKZUAB(dwMaster, app.company, 0);
                base.OnSave();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(app.User + "你", ex.Message);
            }
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["VKZMANDANT"] = app.Mandant;
            dwData["VKZSA"] = VKZSA;
            dwData["VKZAUFKRS"] = KRS;
            dwData["VKZAUFLFD"] = LFD;
            dwData["VKZAUFPOS"] = POS;
            SResult = TMV.QueryVKZUAB(dwData, 2);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }

        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (!IsManage)
            {
                VICC_WFEVENTS.SVWF.ZB02(RUNID, KEY);
            }
            base.ManageCommand(sender, e);
        }

        public void CM2AllCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateVKZUAB(dwMaster, app.company, 2);
        }

        public void ADD2AllCommand(object sender, RoutedEventArgs e)
        {
               if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateVKZUAB(dwMaster, app.company, 3);
        }

        public void RD2AllCommand(object sender, RoutedEventArgs e)
        {
               if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateVKZUAB(dwMaster,  app.company,4);
        }




    }
}
